package com.dhsc.service.impl.ywzj.kbpzgl.wdpzsq;


import com.dhsc.domain.ywzx.kbpzgl.WdlysqlrVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywzj.kbpzgl.wdpzsq.IWdlysqlrDao;
import com.dhsc.service.ywzj.wdpzsq.IWdlysqlrService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WdlysqlrServiceImpl implements IWdlysqlrService {

    @Autowired
    private IWdlysqlrDao wdlysqlrDao;

    @Override
    public LayuiTableVo queryWdlysqlr(WdlysqlrVo w) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(w.getPage(), w.getLimit());
            List<WdlysqlrVo> list = wdlysqlrDao.queryWdlysqlr(w);
            PageInfo<WdlysqlrVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询网点领用申请录入出错",e);
            throw new MyException("查询网点领用申请录入出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryPzgllxrxx(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            WdlysqlrVo wdxx = wdlysqlrDao.queryPzgllxrxx(w);
            obj.setData(wdxx);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("查询联系管理人信息出错",e);
            throw new MyException("查询联系管理人信息出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject saveWdlysqlrxx(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询出最大的申请编号+1
            WdlysqlrVo wdlysqlrVo = wdlysqlrDao.queryWdlysqlrSqbh();
            w.setSqbh(wdlysqlrVo.getSqbh()+1);
            wdlysqlrDao.saveWdlysqlrxx(w);
            //保存申请明细数据
            wdlysqlrDao.saveWdlysqlrSqmxsj(w);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存网点领用申请录入出错",e);
            throw new MyException("保存网点领用申请录入出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryWdlysqlrBjxx(WdlysqlrVo w) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            ArrayList<WdlysqlrVo> objects = new ArrayList<>();
            List<WdlysqlrVo> list = wdlysqlrDao.queryWdlysqlrBjxx(w);
            //查询凭证类型字典
            List<WdlysqlrVo> listzd = wdlysqlrDao.queryPzlxTxtdm(w);
            List<WdlysqlrVo> collect = listzd.stream().distinct().collect(Collectors.toList());
            for (WdlysqlrVo wdlysqlrVo1 : collect) {
                wdlysqlrVo1.setPid("-1");
                wdlysqlrVo1.setId(wdlysqlrVo1.getBm());
                wdlysqlrVo1.setPzbm(wdlysqlrVo1.getBmsm());
            }
            for (WdlysqlrVo wdlysqlrVo : list) {
                wdlysqlrVo.setPid(wdlysqlrVo.getPzlx());
                wdlysqlrVo.setId(wdlysqlrVo.getPzbm());
                wdlysqlrVo.setPzbm(wdlysqlrVo.getPzbm());
            }
            objects.addAll(listzd);
            objects.addAll(list);
            obj.setData(objects);
            obj.setCode(200);
            obj.setCount(objects.size());
            obj.setMsg("");
        }catch (Exception e){
            obj.setCode(500);
            throw new MyException("查询网点领用申请录入编辑信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateSqsl(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            wdlysqlrDao.updateSqsl(w);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("修改申请数量出错",e);
            throw new MyException("修改申请数量出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveWdlysqlrLcxx(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            wdlysqlrDao.saveWdlysqlrLcxx(w);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存网点领用信息流程信息出错",e);
            throw new MyException("查询网点领用申请录入编辑信息出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject deleteWdlysqlr(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            wdlysqlrDao.deleteWdlysqlr(w);
            wdlysqlrDao.deleteWdlysqlrMX(w);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("删除申请单信息出错",e);
            throw new MyException("删除申请单信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject chsqdWdlysqlr(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            wdlysqlrDao.chsqdWdlysqlr(w);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("撤回申请单信息出错",e);
            throw new MyException("撤回申请单信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryWdlyglinfo(WdlysqlrVo w) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(w.getPage(), w.getLimit());
            List<WdlysqlrVo> list = wdlysqlrDao.queryWdlyglinfo(w);
            PageInfo<WdlysqlrVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询网点领用管理信息出错",e);
            throw new MyException("查询网点领用管理信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryJlzt(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            List<WdlysqlrVo> list =  wdlysqlrDao.queryJlzt(w);
            obj.setStatus(200);
            obj.setData(list);
        }catch (Exception e){
            log.error("查询记录状态出错",e);
            throw new MyException("查询记录状态出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateRksjOrBz(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            if(w.getNum() == 1){
                wdlysqlrDao.updateRksj(w);
            }else if (w.getNum() == 2){
                wdlysqlrDao.updateBz(w);
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("修改入库数量或者备注出错",e);
            throw new MyException("修改入库数量或者备注出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveSqdrk(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询是否可以入库
            int a = wdlysqlrDao.querySfrk(w);
            if(a > 0){
                obj.setStatus(400);
                obj.setMessage("入库失败，请确保有编号的凭证都填写了编号范围！");
                return obj;
            }
            wdlysqlrDao.saveSqdrk(w);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("申请单入库出错",e);
            throw new MyException("申请单入库出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateSqdzt(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            wdlysqlrDao.updateSqdzt(w);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("撤回申请单出错",e);
            throw new MyException("撤回申请单出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryExportWdlygl(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            List<WdlysqlrVo> list =null;
            if(w.getBmxz().equals("1")){
                //总部
                list = wdlysqlrDao.queryExportWdlygl(w);
            }else{
                //分部
                list = wdlysqlrDao.queryExportWdlyglFb(w);
            }
            obj.setStatus(200);
            obj.setData(list);
        }catch (Exception e){
            log.error("查询网点申请领用数据出错",e);
            throw new MyException("查询网点申请领用数据出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateJLzt(WdlysqlrVo w) {
        ResponseObject obj = new ResponseObject();
        try {
            if(w.getBmxz().equals("1")){

                wdlysqlrDao.updateZbJLzt(w);
            }else{
                wdlysqlrDao.updateFbJLzt(w);
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("修改记录状态出错",e);
            throw new MyException("修改记录状态出错",e);
        }
        return obj;
    }
}
